// Product detail page
// ===================

define(['require', 'zepto', 'sui'], function(require, $) {
  return {
    init: function(data) {
      this.initGive(data.couponId);
    },
    
    initGive: function(couponId) {
      /*$(document).on('click','.button', function () {
        var modal = $.modal({
          text: '转赠信息',
          afterText: '<input type="text" name="mobile" class="modal-text-input" placeholder="请输入转赠用户手机号"/>'+
                     '<input type="text" name="name" class="modal-text-input" placeholder="请输入转赠用户姓名"/>',
          buttons: [
            {
              text: '取消'
          },
          {
             text: '确认',
             onClick: function () {
               var mobile = $("input[name='mobile']").val();
               if((!/^((13)|(15)|(17)|(18))\d{9}$/.test(mobile) || !mobile)){
                   $.toast('请输入正确的手机号');
               } else {
                 var name = $("input[name='name']").val();
                 if(!name){
                   $.toast('请输入用户姓名');
                 } else{
                   var submitUrl = window.__CTX__.root + '/profile/coupon/' + couponId + '/give';
                   $.post(submitUrl, {
                     'mobile': mobile,
                     'name': name,
                     '_method': 'PATCH'
                   }).done(function(response) {
                     response = JSON.parse(response);
                     if(response.code == 0) {
                       $.toast('转赠成功');
                       setTimeout(function() {
                         location.href = window.__CTX__.root + '/profile/coupons';
                       }, 2000);
                     } else {
                       $.toast(response.message)
                     }
                   });
                 }
               }
             }
          },
        ]
      })
    });*/
      
      
      
      $(document).on('click','.button', function () {
        $.prompt('请输入受赠用户手机号：', function (value) {
          if((!/^((13)|(15)|(17)|(18))\d{9}$/.test(value) || !value)){
            $.toast('请输入正确的手机号');
          } else {
            $.confirm('确认要转赠该优惠券给'+value+'吗?', function () {
              var submitUrl = window.__CTX__.root + '/profile/coupon/' + couponId + '/give';
              $.post(submitUrl, {
                'mobile': value,
                '_method': 'PATCH'
              }).done(function(response) {
                response = JSON.parse(response);
                if(response.code == 0) {
                  $.toast('转赠成功');
                  setTimeout(function() {
                    location.href = window.__CTX__.root + '/profile/coupons';
                  }, 2000);
                } else {
                  $.toast(response.message)
                }
              });
            });
          }
        });
      });
    }
  }
})